package leetcode;

import util.LinkList;
import util.ListNode;

/**
 * @Project : algorithm-learning-java
 * @Package : leetcode
 * @Author : Ruoyu Wang
 * @User : Momenta
 * @DateTime : 2024/1/19 10:05
 */
public class Q0061 {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null || k == 0) return head;
        ListNode tail = head;
        int sum = 1;
        while (tail.next != null) {
            tail = tail.next;
            sum++;
        }
        k = sum - k % sum;
        if (k == sum) return head;
        ListNode p = head;
        while (k > 1) {
            p = p.next;
            k--;
        }
        ListNode s = p.next;
        p.next = null;
        tail.next = head;
        return s;
    }

    public static void main(String[] args) {
        Q0061 sl = new Q0061();
        int[] nodes = {1, 2, 3, 4, 5};
        int k = 2;
        ListNode head = LinkList.createList(nodes);
        LinkList.printList(sl.rotateRight(head, k));
    }
}
